//
//  CountHeight.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/10.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "CountHeight.h"
#import<CommonCrypto/CommonDigest.h>
#import <AVFoundation/AVFoundation.h>

@implementation CountHeight
+(CGFloat)boundingRectWithwidth:(CGFloat)width attributes:(NSDictionary *)attributes context:(NSString *)contextStr;
{
    CGSize size = [contextStr boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    return size.height;
}
//手机号码验证
+ (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *mobile1 = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (mobile1.length != 11)
    {
        return NO;
    }
    else
    {
        NSString *NUM = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",NUM];
        BOOL isMatch = [pred evaluateWithObject:mobile1];
        
        if (isMatch)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
}
//加密
+(NSString *)sha1:(NSString *)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}
/**
 时间戳转时间字符串--年月日格式
 */
+(NSString *)timeWithCut:(NSString *)numTime
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[numTime doubleValue] / 1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:3600*8];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    return currentDateStr;
}
/**
 时间字符串比较大小
 */
+(int)compareDate:(NSString*)date01 withDate:(NSDate*)date02
{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dt1 = [[NSDate alloc]init];
//    NSDate *dt2 = [[NSDate alloc]init];
    dt1 = [df dateFromString:date01];
//    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:date02];
    switch (result)
    {
       //date02比date01大
       case NSOrderedAscending:
            ci=1;
            break;
       //date02比date01小
       case NSOrderedDescending:
            ci=-1;
            break;
       //date02=date01
       case NSOrderedSame:
            ci=0;
            break;
       default:
            NSLog(@"erorr dates %@, %@", date02, dt1);
            break;
    }
    return ci;
}
+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
    
    if(!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
    
    UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;
    CGImageRelease(thumbnailImageRef);
    return thumbnailImage;
}

@end
